package org.awei.service;


import org.awei.dao.IStuDao;
import org.awei.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentService implements IStuService {
    @Autowired
    private IStuDao stuDao;
    @Override
    public ModelAndView query(Student student, Integer pageNo) {
        if (pageNo == null || pageNo < 1) pageNo = 1;
        Integer rowCount = 3;//单页显示行数
        //计算总行数
        Integer totalCount = stuDao.queryCount(student);
        //总页数
        Integer pageCount = 0;
        //计算总页数
        if(totalCount % rowCount == 0){
            pageCount = totalCount / rowCount;
        } else {
            pageCount = (totalCount / rowCount) + 1;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("stu", student);
        map.put("pageNo", (pageNo-1)*rowCount);
        map.put("rowCount", rowCount);
        //查询
        List<Student> list = stuDao.query(map);
        ModelAndView mv = new ModelAndView();
        mv.addObject("list", list);
        mv.addObject("student", student);
        mv.addObject("pageNo", pageNo);
        mv.addObject("pageCount", pageCount);
        return mv;
    }
}
